package com.pharmacy.InventoryManage.service;// 文件位置: src/main/java/com/pharmacy/InventoryManage/service/impl/InventoryServiceImpl.java


import com.pharmacy.InventoryManage.dao.InventoryMapper;
import com.pharmacy.InventoryManage.entity.Inventory;
import com.pharmacy.InventoryManage.service.InventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class InventoryServiceImpl implements InventoryService {

    @Autowired
    private InventoryMapper inventoryMapper;

    @Override
    public List<Inventory> getAllInventories() {
        return inventoryMapper.findAllInventories();
    }

    @Override
    public Inventory getInventoryById(Integer id) {
        return inventoryMapper.findInventoryById(id);
    }

    @Override
    public Inventory getInventoryByMedicineId(Integer medicineId) {
        return inventoryMapper.findInventoryByMedicineId(medicineId);
    }

    @Override
    public List<Inventory> getLowStockInventories() {
        return inventoryMapper.findLowStockInventories();
    }

    @Override
    @Transactional
    public boolean addInventory(Inventory inventory) {
        // 检查是否已存在该药品的库存记录
        Inventory existing = inventoryMapper.findInventoryByMedicineId(inventory.getMedicineId());
        if (existing != null) {
            return false;
        }
        return inventoryMapper.insertInventory(inventory) > 0;
    }

    @Override
    @Transactional
    public boolean updateInventory(Inventory inventory) {
        return inventoryMapper.updateInventory(inventory) > 0;
    }

    @Override
    @Transactional
    public boolean updateInventoryQuantity(Integer medicineId, Integer quantity) {
        return inventoryMapper.updateInventoryQuantity(medicineId, quantity) > 0;
    }

    @Override
    @Transactional
    public boolean deleteInventory(Integer id) {
        return inventoryMapper.deleteInventory(id) > 0;
    }

    @Override
    public boolean checkStock(Integer medicineId, Integer requiredQuantity) {
        Inventory inventory = inventoryMapper.findInventoryByMedicineId(medicineId);
        return inventory != null && inventory.getQuantity() >= requiredQuantity;
    }
}